package com.chinazyjr.framework.web.monitor.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.chinazyjr.framework.common.model.sdto.APIRequest;
import com.chinazyjr.framework.common.model.sdto.APIResult;
import com.chinazyjr.framework.web.base.BaseController;
import com.chinazyjr.framework.web.monitor.manager.logic.MonitorLogic;
import com.chinazyjr.framework.web.monitor.model.dto.ServiceProvider;

@Controller
@RequestMapping("/monitor")
public class MonitorController extends BaseController {

    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(MonitorController.class);

    @Autowired
    private MonitorLogic monitorLogic;

    @RequestMapping(value = "index")
    public ModelAndView index() {
        return new ModelAndView("sys/monitor/index");
    }

    /**
     * 列表
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "providers")
    @ResponseBody
    public APIResult pageList() {
        initParams();
        APIResult result =new APIResult();
        List<ServiceProvider> list=  monitorLogic.getList();
        result.setData(list);
       
        return result;
    }
    /**
     * 服务监控
     * 
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "providers/handshake")
    @ResponseBody
    public APIResult handshake() {
        initParams();
        APIResult result =new APIResult();
        List<ServiceProvider> list=  monitorLogic.connect();
        result.setData(list);
       
        return result;
    }
    
    /**
     * 新增页面跳转
     */
    @RequestMapping(method = RequestMethod.GET, value = "page-add")
    public ModelAndView pageAdd() {

        return new ModelAndView("sys/monitor/add");
    }

    /**
     * 新增
     */
    @RequestMapping(method = RequestMethod.POST, value = "provider")
    @ResponseBody
        public APIResult add() {
          initParams();
          
          APIResult result =new APIResult();
          APIRequest request=this.apiRequest;
          ServiceProvider provider = JSON.parseObject(JSON.toJSONString(request.getDataMap()), ServiceProvider.class);
          monitorLogic.save(provider);
          
        return result;
    }
    
    
    /**
     * 查看
     */
    @RequestMapping(method = RequestMethod.GET, value = "page-view/{providerName}")
    public ModelAndView pageView(@PathVariable("providerName") String providerName) {
        initParams();
        ServiceProvider provider= monitorLogic.get(providerName);
        ModelMap model = new ModelMap();
        model.addAttribute("provider", provider);
        return new ModelAndView("sys/monitor/view", model);
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "provider/{providerName}")
        public APIResult delete(@PathVariable("providerName") String providerName) {
        initParams();
        APIResult result =new APIResult();
        monitorLogic.delete(providerName);
        return result;
    }
    
    /**
     * 跳转到编辑页面
     */
    @RequestMapping(method = RequestMethod.GET, value = "page-edit/{providerName}")
    public ModelAndView pageEdit(@PathVariable("providerName") String providerName) {

        ServiceProvider provider= monitorLogic.get(providerName);
        ModelMap model = new ModelMap();
        model.addAttribute("provider", provider);

        return new ModelAndView("sys/monitor/update", model);
    }

    /**
     * 更新
     */
    @RequestMapping(method = RequestMethod.POST, value = "provider/{providerName}")
    @ResponseBody
    public APIResult edit(@PathVariable("providerName") String providerName) {
        
        APIResult result =new APIResult();
        ServiceProvider provider = new ServiceProvider();
        provider.setProviderName(providerName);
        monitorLogic.update(provider);
        
        return result;
    }

}
